package view
{
	import event.AssetsEvent;
	
	import flash.display.BitmapData;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.utils.Dictionary;
	
	import util.AnimationFactory;
	import util.AssetsManager;
	
	import vo.ViewObject;
	
//	import com.greensock.TweenLite;
	
	public class HeroView extends Sprite
	{
		private var _vo:ViewObject;
		
		private var _animationObj:AnimationObj;
		
		public var state:int = -1;//-1 没准备好，0 run，1 jump，2 trundle，3 revolve 
		
		public static const RUN:String = "run";
		public static const JUMP:String = "jump";
		public static const TRUNDLE:String = "trundle";
		public static const REVOLVE:String = "revolve";
		
		private var actionArr:Array = [
										{"action":RUN,"row":0,"frameRate":40},
										{"action":JUMP,"row":1,"frameRate":180},
										{"action":TRUNDLE,"row":2,"frameRate":80},
										{"action":REVOLVE,"row":3,"frameRate":80}
									  ];
		
		private var actionDic:Dictionary = new Dictionary();
		
		public function HeroView(viewObject:ViewObject=null)
		{
			super();
			roleView = viewObject;
			_animationObj = new AnimationObj();
			this.addChild(_animationObj);
		}
		
		public function set roleView(roleView:ViewObject):void {
			this._vo = roleView;
			AssetsManager.instance.loadImage(roleView.source,"hero");
			AssetsManager.instance.addEventListener("loadComplete",onLoadComplete);
		}
		
		public function onLoadComplete(e:AssetsEvent):void {
			if(e.id=="hero") {
				var source:BitmapData = e.loadedBitmapData;
				
				for each (var frameObj:Object in actionArr) {
					_animationObj.setframe(frameObj.action,AnimationFactory.instance.cutBitmapDataArray(source,frameObj.row,8,source.width/8,source.height/8));
					actionDic[frameObj.action] = frameObj.frameRate;
				}
				
				this.dispatchEvent(new Event("ready"));
				state = 0;
			}
		}
		
		public function action(frame:Object):void {
			_animationObj.frame = frame;
			_animationObj.frameRate = actionDic[frame];
			
			switch(frame) {
				case RUN:
					break;
				case JUMP:
//					TweenLite.to(this,1,{y:this.y - _vo.jumpHigh,onComplete:onCompelte()});
					break;
			}
			
			_animationObj.play();
		}
		
		public function onCompelte():void {
//			TweenLite.to(this,1,{y:this.y + _vo.jumpHigh});
		}
		
		public function get animationObj():AnimationObj {
			return _animationObj;
		}
		
		public function get vo():ViewObject {
			return this._vo;
		}
	}
}